//
//  GLRootViewController.swift
//  GLWeiBo
//
//  Created by mac on 16/5/10.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import HMRefresh
class GLRootViewController: UIViewController {
    let userLogon = true
    lazy var tableView = UITableView(frame: CGRectZero, style:.Plain)
    //下拉刷新空间
//    var refresh: UIRefreshControl?
    lazy var refresh = HMRefreshControl()
    var pullupView: UIActivityIndicatorView?
    //访客视图：
    var visitorView: GLView?
    override func loadView() {
        super.loadView()
        userLogon ? prepareTableView(): setupVisitorView()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()
    }
    //:公共方法，供子类实现
    func loadData() {
    }
    //结束刷新
//    func endRefreshing() {
//        refresh?.endRefreshing()
//        pullupView?.stopAnimating()
//    }
}
extension GLRootViewController: UITableViewDataSource, UITableViewDelegate {
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        return UITableViewCell()
    }
//    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath){
//        let section = tableView.numberOfSections - 1
//        if section < 0 {
//            return
//        }
//        let row = tableView.numberOfRowsInSection(section) - 1
//        if indexPath.section == section && indexPath.row == row && pullupView?.isAnimating() == false{
//            print("上拉刷新")
//            pullupView?.startAnimating()
//            
//            loadData()
//        }
//
//    }
}
extension GLRootViewController {
   @objc private func visitorViewClickRegistorButton() {
        print("点击了注册按钮")
    }
   @objc private func visitorViewClickLoginButton() {
        print("点击了登录按钮")
    }
}
extension GLRootViewController {
    private func setupVisitorView() {
        visitorView = GLView()
        view = visitorView
        navigationItem.leftBarButtonItem = UIBarButtonItem(
            title: "注册",
            style: UIBarButtonItemStyle.Plain,
            target: self,
            action: "visitorViewClickRegistorButton")
        navigationItem.rightBarButtonItem = UIBarButtonItem(
            title: "登录",
            style: UIBarButtonItemStyle.Plain,
            target: self,
            action: "visitorViewClickLoginButton")
        visitorView?.regesterButton.addTarget(self,
            action: "visitorViewClickRegistorButton", forControlEvents:
            UIControlEvents.TouchUpInside)
        visitorView?.loginButton.addTarget(self,
            action: "visitorViewClickLoginButton", forControlEvents:
            UIControlEvents.TouchUpInside)

        view.backgroundColor = UIColor.whiteColor()
        
    }
    private func prepareTableView() {
        view = tableView
        tableView.dataSource = self
        tableView.delegate = self
    }
    ///准备刷新控件
    func prepareRefreshControl() {
        tableView.addSubview(refresh)
        tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0)
        refresh.normalString = "下拉起飞"
        refresh.pullingString = "放开起飞"
        refresh.refreshingString = "正在起飞"
        refresh.lastRefreshString = "末次 "

        refresh.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
//        view.addSubview(refresh)
//        pullupView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
//        pullupView?.color=UIColor.grayColor()
//        tableView.tableFooterView = pullupView

    }
}
